package com.springboot.huli.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.huli.entity.Hushi;
import com.springboot.huli.entity.SysUser;
import com.springboot.huli.entity.UserRole;
import com.springboot.huli.entity.vo.HushiVo;
import com.springboot.huli.mapper.HushiMapper;
import com.springboot.huli.qpage.QPage;
import com.springboot.huli.service.IHushiService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.huli.service.ISysUserService;
import com.springboot.huli.service.IUserRoleService;
import com.springboot.huli.util.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 护士表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-03-28
 */
@Service
public class HushiServiceImpl extends ServiceImpl<HushiMapper, Hushi> implements IHushiService {

    @Autowired
    private ISysUserService iSysUserService;

    @Autowired
    private IUserRoleService iUserRoleService;

    /**
     * 分页查询分类
     * @param qPage
     * @return
     */
    @Override
    public IPage getPageList(QPage qPage, HushiVo obj) {
        IPage iPage = new Page(qPage.getPage(), qPage.getRows());
        List<HushiVo> mapList = this.baseMapper.getPageList(iPage,obj);
        return iPage.setRecords(mapList);
    }

    /**
     * 新增
     * @param obj
     * @return
     */
    @Override
    public Result insert(HushiVo obj) {
        //验证当前账号是否存在
        QueryWrapper<SysUser> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("account",obj.getAccount());
        queryWrapper.eq("type",3);
        if(this.iSysUserService.getOne(queryWrapper)!=null){
            return Result.reError("账号已存在，请重新更换账号");
        }
        SysUser sysUser=new SysUser();
        sysUser.setAccount(obj.getAccount());
        sysUser.setPwd(obj.getPwd());
        sysUser.setCtime(new Date());
        sysUser.setType("3");
        this.iSysUserService.save(sysUser);
        Hushi hushi=new Hushi();
        BeanUtils.copyProperties(obj,hushi);
        hushi.setUserid(sysUser.getUserid());
        this.save(hushi);
        UserRole userRole=new UserRole();
        userRole.setRoleid(3);
        userRole.setUserid(sysUser.getUserid());
        this.iUserRoleService.save(userRole);
        return Result.reOk("恭喜，新增成功");
    }

    /**
     * 修改
     * @param obj
     * @return
     */
    @Override
    public Result updates(HushiVo obj) {
        Hushi hushi=this.getById(obj.getUserid());
        BeanUtils.copyProperties(obj,hushi,new String[]{"userid"});
        this.updateById(hushi);
        SysUser sysUser=this.iSysUserService.getById(obj.getUserid());
        sysUser.setPwd(obj.getPwd());
        this.iSysUserService.updateById(sysUser);
        return Result.reOk("恭喜，修改成功");
    }


}
